package com.dmgis.qxfw.dataservice.businessservice.yyrb.service.Impl;

import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.RequestDataBySelect;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.important_weather;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.smallscale_station;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.township_forecasttemp;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.zhongbao_product;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.HourFcst1;
import com.dmgis.qxfw.dataservice.businessservice.common.util.DateTime;
import com.dmgis.qxfw.dataservice.businessservice.common.util.JsonUtil;
import com.dmgis.qxfw.dataservice.businessservice.common.util.StringUtil;
import com.dmgis.qxfw.dataservice.businessservice.controller.ProcessRequestController;
import com.dmgis.qxfw.dataservice.businessservice.yyrb.service.ICityFcstService;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("cityfcstService")
public class CityFcstService implements ICityFcstService {
	ProcessRequestController process = new ProcessRequestController();
	
	@Override
	public String Init08TempLoadTime() {
		// TODO 初始化08时预报最新时间
		township_forecasttemp listdata = new township_forecasttemp();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		selectRequest.setColumns(new String[] { "datechar" });
		selectRequest.setParam(String.format(" ntimes='24' and timechar='08'"));
		selectRequest.setSort(new String[] { "datechar desc" });
		selectRequest.setLimit(1);
		try {
			selectParam.put("method", "township_forecasttemp");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = process.processRequest(selectParam);
			System.out.println(message);
			listdata = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<township_forecasttemp>>() {
			}).get(0);
		} catch (Exception e) {
			listdata.setDatechar(DateTime.now().toDate());
		}
		String newestDate = new DateTime(listdata.getDatechar()).toDateTimeString("yyyy-MM-dd");
		return newestDate;
	}

	@Override
	public String InitHourForecast1Time() {
		// TODO 08时文档最新时间
		zhongbao_product listdata = new zhongbao_product();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		selectRequest.setColumns(new String[] { "forecasttime" });
		selectRequest.setParam(String.format(" producttype='08时天气预报'"));
		selectRequest.setSort(new String[] { "forecasttime desc" });
		selectRequest.setLimit(1);
		try {
			selectParam.put("method", "zhongbao_product");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = process.processRequest(selectParam);
			System.out.println(message);
			listdata = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<zhongbao_product>>() {
			}).get(0);
		} catch (Exception e) {
			listdata.setForecasttime(DateTime.now().toDate());
		}
		String newestDate = new DateTime(listdata.getForecasttime()).toDateTimeString("yyyy-MM-dd");
		return newestDate;
	}

	@Override
	public List<HourFcst1> hourForecast1(String strStartTime, String strEndTime) {
		// TODO 08时天气预报产品
		List<zhongbao_product> listData = new ArrayList<zhongbao_product>();
		List<HourFcst1> resultList = new ArrayList<HourFcst1>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		selectRequest.setColumns(new String[] { "id","productname","producttype" });// 条件where
		StringBuilder strWhere = new StringBuilder();
		if (!StringUtil.IsNullOrEmpty(strStartTime)) {
			strWhere.append(String.format(" forecasttime>='%s'", strStartTime));
		}
		if (!StringUtil.IsNullOrEmpty(strEndTime)) {
			strWhere.append(String.format(" and forecasttime<='%s'", strEndTime));
		}
		strWhere.append(String.format(" and producttype='08时天气预报'"));
		selectRequest.setParam(strWhere.toString());
		selectRequest.setSort(new String[] { "forecasttime desc" });
		selectRequest.setLimit(1);
		try {
			selectParam.put("method", "zhongbao_product");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = process.processRequest(selectParam);
			System.out.println(message);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<zhongbao_product>>() {
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (listData != null && listData.size() > 0) {
			for (zhongbao_product zb : listData) {
				HourFcst1 fcst = new HourFcst1();
				fcst.setId(zb.getId().toString());
				fcst.setProductname(zb.getProductname());
				resultList.add(fcst);
			}
		}
		return resultList;
	}

	@Override
	public important_weather getIWNewDate() {
		// TODO 重要天气时间
		important_weather listdata = new important_weather();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		selectRequest.setColumns(new String[] { "observetime" });
		selectRequest.setSort(new String[] { "observetime desc" });
		selectRequest.setLimit(1);
		try {
			selectParam.put("method", "important_weather");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = process.processRequest(selectParam);
			System.out.println(message);
			listdata = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<important_weather>>() {
			}).get(0);
		} catch (Exception e) {
			listdata.setObservetime(DateTime.now().toDate());
		}
		return listdata;
	}

	@Override
	public List<important_weather> getimportantweather(String startDate, String endDate) {
		// TODO 重要天气
		List<important_weather> listData = new ArrayList<important_weather>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<smallscale_station> impWeatherList = this.getSmallScaleStationName();
		// 条件where
		StringBuilder strWhere = new StringBuilder();
		if (!StringUtil.IsNullOrEmpty(startDate)) {
			strWhere.append(String.format(" observetime>='%s'", startDate));
		}
		if (!StringUtil.IsNullOrEmpty(endDate)) {
			strWhere.append(String.format(" and observetime<='%s'", endDate));
		}
		if (impWeatherList != null) {
			strWhere.append(" and stationname in (");
			String tempString = "";
			for (smallscale_station weather : impWeatherList) {
				tempString += String.format("'%s',", weather.getStationname());
			}
			strWhere.append(tempString.substring(0, tempString.length() - 1) + ")");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "important_weather");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = process.processRequest(selectParam);
			System.out.println(message);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<important_weather>>() {
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listData;
	}

	/**
	 * 查询常德关联站点的站点名
	 * @return
	 */
	public List<smallscale_station> getSmallScaleStationName(){
		Map<String, String> selectParam = new HashMap<String, String>();
		List<smallscale_station> impWeatherList = new ArrayList<>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		selectRequest.setColumns(new String[]{"DISTINCT stationname"});
		// 条件where
		selectRequest.setParam(" city like '%"+DbConfig.CITY+"%' and stationid like '5%%' ");
		try {
			selectParam.put("method", "smallscale_station");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = process.processRequest(selectParam);
			System.out.println(message);
			impWeatherList = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<smallscale_station>>() {
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return impWeatherList;
	}

}
